package neusoft.yuejian_11.model;

import java.util.List;

import neusoft.yuejian_11.bean.AttentionBean;
import neusoft.yuejian_11.bean.ClubBean;
import neusoft.yuejian_11.bean.CourseBean;
import neusoft.yuejian_11.bean.VideoBean;
import neusoft.yuejian_11.interF.AttentionService;
import neusoft.yuejian_11.interF.Const;
import neusoft.yuejian_11.listener.RetrofitListener;
import neusoft.yuejian_11.interF.RetrofitService;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * Created by neusoft on 2017/9/29.
 */

public class ClubModel {
    private Retrofit retrofit;
    public ClubModel(){
        retrofit = new Retrofit.Builder()
                .baseUrl(Const.BASE_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
    }
    //俱乐部列表
    public void  clubList(final RetrofitListener listener){
        RetrofitService retrofitService = retrofit.create(RetrofitService.class);
        Call<List<ClubBean>> call = retrofitService.getAllClubs();
        Callback<List<ClubBean>> callback = new Callback<List<ClubBean>>() {
            @Override
            public void onResponse(Call<List<ClubBean>> call, Response<List<ClubBean>> response) {
                List<ClubBean> clubBeanList = response.body();
                listener.onSuccess(clubBeanList,1);

            }

            @Override
            public void onFailure(Call<List<ClubBean>> call, Throwable t) {
                listener.onFail();

            }
        };
        call.enqueue(callback);
    }
    //俱乐部详情
    public void clubDetail(final String clubId, final RetrofitListener listener){
        RetrofitService retrofitService = retrofit.create(RetrofitService.class);
        Call call = retrofitService.getClubById(clubId);
        Callback<ClubBean> callback = new Callback<ClubBean>() {
            @Override
            public void onResponse(Call<ClubBean> call, Response<ClubBean> response) {
                ClubBean clubBeanDetail = response.body();
                listener.onSuccess(clubBeanDetail,1);
            }

            @Override
            public void onFailure(Call<ClubBean> call, Throwable t) {
                listener.onFail();
            }
        };
        call.enqueue(callback);
    }
    //俱乐部课程列表
    public void courseList(String clubId, final RetrofitListener listener){
    RetrofitService retrofitService = retrofit.create(RetrofitService.class);
    Call<List<CourseBean>> call = retrofitService.getCourseByClub(clubId);
    Callback<List<CourseBean>> callback = new Callback<List<CourseBean>>() {
        @Override
        public void onResponse(Call<List<CourseBean>> call, Response<List<CourseBean>> response) {
            List<CourseBean> courseBeanList = response.body();
            listener.onSuccess(courseBeanList,1);
        }

        @Override
        public void onFailure(Call<List<CourseBean>> call, Throwable t) {
            listener.onFail();

        }
    };
        call.enqueue(callback);
}
    //俱乐部视频列表
    public void videoList(String clubId, final RetrofitListener listener){
        RetrofitService retrofitService = retrofit.create(RetrofitService.class);
        Call<List<VideoBean>> call = retrofitService.getVideoByClub(clubId);
        Callback<List<VideoBean>> callback = new Callback<List<VideoBean>>() {
            @Override
            public void onResponse(Call<List<VideoBean>> call, Response<List<VideoBean>> response) {
                List<VideoBean> videoBeanList = response.body();
                listener.onSuccess(videoBeanList,1);
            }

            @Override
            public void onFailure(Call<List<VideoBean>> call, Throwable t) {
                listener.onFail();
            }
        };
        call.enqueue(callback);
    }
    //搜索俱乐部
    public void clubSearch(String clubName, final RetrofitListener listener) {
        RetrofitService retrofitService = retrofit.create(RetrofitService.class);
        Call<List<ClubBean>> call = retrofitService.getClubBySearch(clubName);
        Callback<List<ClubBean>> callBack = new Callback<List<ClubBean>>() {
            @Override
            public void onResponse(Call<List<ClubBean>> call, Response<List<ClubBean>> response) {
                List<ClubBean> clubBeanList = response.body();
                listener.onSuccess(clubBeanList,1);

            }

            @Override
            public void onFailure(Call<List<ClubBean>> call, Throwable t) {
                listener.onFail();

            }
        };
        call.enqueue(callBack);
    }
    //收藏俱乐部
    public void favorClub(int userId, String clubId, final RetrofitListener listener){
        AttentionService attentionService = retrofit.create(AttentionService.class);
        Call<AttentionBean> call = attentionService.userCollectClub(userId,clubId);
        Callback<AttentionBean> callback = new Callback<AttentionBean>() {
            @Override
            public void onResponse(Call<AttentionBean> call, Response<AttentionBean> response) {
                AttentionBean attentionBean = response.body();
                listener.onSuccess(attentionBean,2);
            }

            @Override
            public void onFailure(Call<AttentionBean> call, Throwable t) {
                listener.onFail();
            }
        };
        call.enqueue(callback);
    }
}
